package com.work.dyduser.mvp.ui;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.work.dyduser.R;
import com.work.dyduser.mvp.base.BaseMVPActivity;
import com.work.dyduser.mvp.contract.PlayVideoContract;
import com.work.dyduser.mvp.presenter.PlayVideoPresenter;
import com.work.dyduser.utils.UIUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: Wangzhan
 * date :2018/9/19 0019 09:48
 * version:2.0.0
 * description:上传视频
 */

public class PlayVideoActivity extends BaseMVPActivity<PlayVideoContract.View, PlayVideoPresenter> implements PlayVideoContract.View {

    @BindView(R.id.tv_delete)
    TextView tvdelete;
    @BindView(R.id.tv_upload)
    TextView tvupload;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.iv_play)
    ImageView playVideo;
    @BindView(R.id.surfaceview)
    SurfaceView videoSurfaceView;
    private String path;
    private MediaPlayer mediaPlayer;

    private int i;

    @Override
    public PlayVideoPresenter initPresenter() {
        return new PlayVideoPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pagetwo_playvideo;
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("上传视频");
        mTitleBar.titleRight.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        path = getIntent().getExtras().getString("videoPath");

        MediaMetadataRetriever media = new MediaMetadataRetriever();
        if (path != null) {
            media.setDataSource(path);
            Bitmap bitmap = media.getFrameAtTime();
            imageView.setImageBitmap(bitmap);
        }


    }


    @Override
    public void showLoading() {
        showLoaddialog("上传中");
    }

    @Override
    public void hideLoading() {
        hideLoaddialog();
    }

    @Override
    public void onSuccess(int type, Object bean) {

    }

    @Override
    public void onfailed(int errcode, String error) {

    }

    @Override
    protected void onStop() {
        super.onStop();
        //暂停播放
        stopPlayVideo();
    }


    /**
     * 播放视频
     */
    public void playVideo() {
        imageView.setVisibility(View.GONE);
        videoSurfaceView.setVisibility(View.VISIBLE);
        playVideo.setVisibility(View.GONE);
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(path);
            mediaPlayer.setDisplay(videoSurfaceView.getHolder());
            mediaPlayer.prepare();//缓冲
            mediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                playVideo.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);
                videoSurfaceView.setVisibility(View.GONE);
            }
        });
    }


    /**
     * 暂停播放
     */
    public void stopPlayVideo() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }


    @OnClick({R.id.iv_play, R.id.tv_delete, R.id.tv_upload})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.iv_play:
                playVideo();
                break;
            case R.id.tv_delete:
                finish();
                break;
            case R.id.tv_upload:

                myPresenter.UploadVideo(path);

                UIUtils.getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        hideLoading();
                        UIUtils.showToast("上传成功,祝您工作愉快");
                        finish();
                    }
                }, 2000);
                break;
            default:
                break;
        }
    }

}
